package com.monster;

import com.lmax.disruptor.BlockingWaitStrategy;
import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.ProducerType;
import com.lmax.disruptor.util.DaemonThreadFactory;
import com.monster.consumer.ApplicationConsumer;
import com.monster.consumer.JournalConsumer;
import com.monster.consumer.ReplicationConsumer;
import com.monster.event.LongEvent;
import com.monster.event.LongEventFactory;
import com.monster.producor.LongEventProducer;

/**
 * description:
 *
 * @author Yuanml on 2021/4/13 20:05
 */
public class LongEventMain {
    public static void main(String[] args) throws InterruptedException {
        // 1、创建RingBuffer中事件元素的工厂对象
        LongEventFactory factory = new LongEventFactory();

        // 2、指定RingBuffer的大小，必须为2的幂次方
        int bufferSize = 1024;

        // 3、构造Disruptor
        Disruptor<LongEvent> disruptor = new Disruptor<LongEvent>(factory, bufferSize, DaemonThreadFactory.INSTANCE, ProducerType.SINGLE, new BlockingWaitStrategy());

        // 4、注册消费者
        disruptor.handleEventsWith(new JournalConsumer(), new ReplicationConsumer(), new ApplicationConsumer());

        // 5、启动Disruptor
        disruptor.start();

        // 6、从Disruptor中获取RingBuffer
        RingBuffer<LongEvent> ringBuffer = disruptor.getRingBuffer();

        // 7、创建生产者
        LongEventProducer producer = new LongEventProducer(ringBuffer);

        // 8、生产元素，并放入RingBuffer
        for (int i = 0; i < 100; i++) {
            producer.onData(i);
            Thread.sleep(1000);
        }

        // 9、挂起当前线程
        Thread.currentThread().join();
    }

}
